import { app } from 'electron'
import fs from 'fs';
import path, { join } from 'path'
// path.dirname(app.getPath('exe')) 项目安装目录

const filePathArr = {
    '1': process.env.NODE_ENV === 'development' ? 'public/db/scheduleDB.json' : join(path.dirname(app.getPath('exe')), 'db/scheduleDB.json'),
    '2': process.env.NODE_ENV === 'development' ? 'public/db/noteDB.json' : join(path.dirname(app.getPath('exe')), 'db/noteDB.json'),
    '3': process.env.NODE_ENV === 'development' ? 'public/db/settingDB.json' : join(path.dirname(app.getPath('exe')), 'db/settingDB.json')
}

/**
 * 读取文件
 * @param type 1-日程 2-便签 3-设置
 * @returns 
 */
export const readdbs = (type: any = 3) => {
    return new Promise((resolve, reject) => {
        const filePath = filePathArr[type];
        fs.readFile(`${filePath}`, (err, data) => {
            if (err) {
                reject(err.toString())
            }
            else{
                resolve(data.toString());
            }
        })
    })
}
/**
 * 写入文件
 * @param data 要写入的数据
 * @param type 1-日程 2-便签 3-设置
 * @returns 
 */
export const writedbs = (data: string, type: any = 3) => {
    return new Promise((resolve, reject) => {
        const filePath = filePathArr[type];
        fs.writeFile(filePath, data, (err) => {
            if (err) {
                reject(false)
            } else {
                resolve(true);
            }
        });
    })
}